package com.woniuxy.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.admin.exception.AdminException;
import com.woniuxy.admin.entity.AdminAccountRole;
import com.woniuxy.admin.entity.AdminRole;
import com.woniuxy.admin.mapper.AdminAccountRoleMapper;
import com.woniuxy.admin.mapper.AdminRoleMapper;
import com.woniuxy.admin.service.AdminRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ...
 * @since 2024年05月22日
 */
@Service
public class AdminRoleServiceImpl extends ServiceImpl<AdminRoleMapper, AdminRole> implements AdminRoleService {

    @Resource
    private AdminAccountRoleMapper adminAccountRoleMapper;

    /**
     * 根据账号Id查找拥有的角色
     * @param accountId 账号Id
     * @return 角色实体
     */
    @Override
    public AdminRole loadAllRoleByAccountId(Integer accountId) {
       AdminAccountRole adminAccountRole = adminAccountRoleMapper.selectOne(new QueryWrapper<AdminAccountRole>().eq("account_id", accountId));
        if(adminAccountRole==null) throw new AdminException(20001,"该账户还未拥有角色");
        AdminRole adminRole = this.getById(adminAccountRole.getRoleId());
        return adminRole;
    }

    /**
     * 根据账号Id查找拥有的角色名
     *
     * @param accountId 账号Id
     * @return string
     */
    @Override
    public String loadAllRoleNameByAccountId(Integer accountId) {
        AdminAccountRole adminAccountRole = adminAccountRoleMapper.selectOne(new QueryWrapper<AdminAccountRole>().eq("account_id", accountId));
        if(adminAccountRole==null) {
            return "未分配角色";
        }
        AdminRole adminRole = this.getById(adminAccountRole.getRoleId());
        return adminRole.getRoleName();
    }

    /**
     * 根据账号查询roleId
     *
     * @param accountId
     * @return
     */
    @Override
    public Integer selectRoleId(Integer accountId) {
        AdminAccountRole adminAccountRole = adminAccountRoleMapper.selectOne(new QueryWrapper<AdminAccountRole>().eq("account_id", accountId));
        if(adminAccountRole==null) throw new AdminException(10001,"未拥有角色");
        return adminAccountRole.getRoleId();
    }
}
